const wx2my = require('../../wx2my');

// pages/order_management/order_management.js
const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    keyword: '',
    tab: [{
      "name": "进行中订单",
      "index": 0,
      "select": 1,
      "type": 3
    }, {
      "name": "已完成订单",
      "index": 1,
      "select": 0,
      "type": 1
    }],
    type: 3,
    continue: true,
    abnormal: true,
    stopMoney: '',
    price: '',
    toReturnOrder: 'toReturnOrder',
    toStopOrder: 'toStopOrder'
  },
  onLoad: function (e) {},
  onReady: function () {},
  onShow: function () {
    page = 1;
    this.get_list();
  },

  /*顶部tab切换 */
  active(e) {
    if (e.currentTarget.dataset.select == 1) {
      return false;
    }

    var that = this;
    let tab = that.data.tab;
    tab.forEach((item, index) => {
      if (index == e.currentTarget.dataset.index) {
        item.select = 1;
      } else {
        item.select = 0;
      }
    });
    let type = e.currentTarget.dataset.type;
    that.setData({
      tab: tab,
      type: type
    });
    page = 1;
    that.get_list();
  },

  searchInput(e) {
    console.log(e);
    this.setData({
      keyword: e.detail.value
    });

    if (e.detail.value == '') {
      page = 1;
      this.get_list();
    }
  },

  search() {
    let that = this;

    if (that.data.keyword == '') {
      app.toast('请输入订单号');
    } else {
      page = 1;
      that.get_list();
    }
  },

  stopInput(e) {
    let that = this;
    that.setData({
      stopMoney: e.detail.value
    });
  },

  returnInput(e) {
    let that = this;
    that.setData({
      price: e.detail.value
    });
  },

  get_list() {
    let that = this;
    app.wxRequest('order_list', {
      'shop_token': wx2my.getStorageSync('shop_token'),
      'page': page,
      'keyword': that.data.keyword,
      'status': that.data.type
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let list = res.data.data.data.list;
        console.log(list);

        if (page == 1) {
          totalpage = res.data.data.data.totalpage;
        }

        that.setData({
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false,
          order_total: res.data.data.total_order,
          money: res.data.data.total_money,
          back_num: res.data.data.back_num,
          have_back: res.data.data.have_back
        });
      }
    });
  },

  return(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    that.setData({
      id: id,
      abnormal: false
    });
  },

  stop(e) {
    let that = this;
    let trade_id = e.currentTarget.dataset.trade_id;
    that.setData({
      trade_id: trade_id,
      continue: false
    });
  },

  toStopOrder() {
    let that = this;
    that.setData({
      toStopOrder: ''
    });
    app.wxRequest('end_order', {
      'shop_token': wx2my.getStorageSync('shop_token'),
      'trade_id': that.data.trade_id,
      'money': that.data.stopMoney
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
        that.setData({
          continue: true,
          type: 3,
          stopMoney: '',
          toStopOrder: 'toStopOrder'
        });
        page = 1;
        that.get_list();
      } else {
        that.setData({
          toStopOrder: 'toStopOrder'
        });
        app.toast(res.data.msg);
      }
    });
  },

  toReturnOrder() {
    let that = this;
    that.setData({
      toReturnOrder: ''
    });
    app.wxRequest('manual_refund', {
      'shop_token': wx2my.getStorageSync('shop_token'),
      'trade_id': that.data.id,
      'money': that.data.price
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
        that.setData({
          abnormal: true,
          toReturnOrder: 'toReturnOrder',
          price: '',
          type: 1
        });
        page = 1;
        that.get_list();
      } else {
        that.setData({
          toReturnOrder: 'toReturnOrder'
        });
        app.toast(res.data.msg);
      }
    });
  },

  del() {
    this.setData({
      continue: true,
      abnormal: true,
      price: '',
      money: ''
    });
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    page++;

    if (!this.data.lock) {
      this.get_list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});